Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Calendar.Components
Imports C4F.DevKit.Calendar

Namespace C4F.DevKit.Calendar.Samples
	Public Partial Class SelectAppointmentDialog
		Inherits Form
		Public Sub New(ByVal calService As CalendarService)
			InitializeComponent()

			appointmentsList1.CalendarService = calService
			calendarService1 = calService
			appointmentsList1.ContextMenuStrip = contextMenuStrip1

			If (Not DesignMode) Then
				appointmentsList1.RefreshList()
				Text = String.Format("{0} - {1}", Path.GetFileNameWithoutExtension(calendarService1.CalendarFilePath), Text)
			End If
		End Sub

		Public Sub RefreshList()
			appointmentsList1.RefreshList()
		End Sub

		Private Sub btnNewAppt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNewAppt.Click
			Dim apptDialog As AppointmentDialog = New AppointmentDialog(calendarService1)
			apptDialog.MdiParent = Me.MdiParent
			apptDialog.Show()
		End Sub

		Private Sub btnEditAppt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEditAppt.Click
			EditSelectedAppointment()
		End Sub

		Private Sub EditSelectedAppointment()
			If appointmentsList1.SelectedAppointment Is Nothing Then
				MessageBox.Show("Please select an appointment or create a new appointment")
				Return
			End If

			Dim apptDialog As AppointmentDialog = New AppointmentDialog(calendarService1, appointmentsList1.SelectedAppointment)
			apptDialog.MdiParent = Me.MdiParent
			apptDialog.Show()
		End Sub

		Private Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImport.Click
			Try
				calendarService1.ImportCalendar()
				appointmentsList1.RefreshList()
			Catch e1 As Exception
				MessageBox.Show(String.Format("Could not open calendar {0}. Please choose a different file", calendarService1.CalendarFilePath), "Error while trying to open calendar")
			End Try
		End Sub

		Private Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
			Dim exportTarget As String = calendarService1.PromptForSaveLocation()

			If String.IsNullOrEmpty(exportTarget) Then
				Return
			End If

			Try
				calendarService1.ExportCalendar(exportTarget)
			Catch e1 As Exception
				MessageBox.Show(String.Format("Could not write to file {0}. Please choose a different file to save to.", calendarService1.CalendarFilePath), "Error while exporting calendar")
			End Try
		End Sub

		Private Sub exportAppointmentToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles exportAppointmentToolStripMenuItem.Click
			Dim filePath As String = calendarService1.PromptForOpenLocation()

			If String.IsNullOrEmpty(filePath) Then
				Return
			End If

			calendarService1.Export(appointmentsList1.SelectedAppointment, filePath)
		End Sub

		Private Sub importAppointmentToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles importAppointmentToolStripMenuItem.Click
			Dim newCalFilePath As String = calendarService1.PromptForOpenLocation()

			If String.IsNullOrEmpty(newCalFilePath) Then
				Return
			End If

			Try
				Dim importDialog As ImportAppointmentDialog = New ImportAppointmentDialog(Me, newCalFilePath)
				importDialog.MdiParent = Me.MdiParent
				importDialog.Show()
			Catch e1 As Exception
				MessageBox.Show(String.Format("Error while trying to load calendar {0}. Please select another calendar file"), "Error while loading calendar")
			End Try
		End Sub

		Public Sub Import(ByVal appt As Appointment)
			calendarService1.Import(appt, calendarService1.CalendarFilePath)
			appointmentsList1.RefreshList()
		End Sub
	End Class
End Namespace
